package at.codebase.web.templates;

import static at.codebase.web.util.i18n.I18NFactory.i18n;

import java.util.ArrayList;

import at.codebase.core.Settings;
import at.codebase.security.SecurePrintWriter;
import at.codebase.security.SecureStringBuilder;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.URLBuilder;
import at.codebase.web.util.i18n.I18NLocale;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * This Template represents the (project specific) main side structure. That
 * will include Meta-Tags, Javascript libraries, charset and other important
 * stuff. It could only be used with an LocalRequest object.
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 * 
 */
public class MainTemplate extends Template {

	private static String TEMPLATE_URL = "site_main_layout.html";
	
	private ArrayList<String> jsFiles = new ArrayList<String>();

	public MainTemplate(RequestInformation reqInfo) {
		super(TemplateEngine.getTemplateContent(TEMPLATE_URL));
		init(reqInfo);
	}

	public MainTemplate(RequestInformation reqInfo, SecurePrintWriter writer) {
		super(TemplateEngine.getTemplateContent(TEMPLATE_URL), writer);
		init(reqInfo);
	}

	private void init(RequestInformation reqInfo) {
		// User
		UserService userService = UserServiceFactory.getUserService();
		//User user = userService.getCurrentUser();

		// UserWelcome
		if (reqInfo.isUserLoggedIn()) {
			this.setHTML(
					"userWelcome",
					(new SecureStringBuilder()
							.appendSecure(i18n(reqInfo.getLocale()).string("bar_userWelcome"))
							.appendInsecure("<b>")
							.appendSecure(reqInfo.getUser().getUsername())
							.appendInsecure("</b>")).toString());
		} else {
			this.setHTML("userWelcome", "");
		}
		//UserMenu
		SecureStringBuilder userMenu = new SecureStringBuilder();
		if (reqInfo.isUserLoggedIn()) {
			//Profil Link
			userMenu.appendInsecure("<a href=\"");
			userMenu.appendInsecure(URLBuilder.create("/user/"+reqInfo.getUser().getId()).get());
			userMenu.appendInsecure("\">");
			userMenu.appendSecure(
					i18n(reqInfo.getLocale())
							.string("bar_userProfileButton"));
			userMenu.appendInsecure("</a>");
			userMenu.appendSecure(" | ");
			//Messages Link
			userMenu.appendInsecure("<a href=\"");
			userMenu.appendInsecure(URLBuilder.create("/user/profile/messages").get());
			userMenu.appendInsecure("\">");
			userMenu.appendSecure(
					i18n(reqInfo.getLocale())
							.string("bar_messagesButton"));
			userMenu.appendInsecure("</a>");
			userMenu.appendSecure(" | ");
			//Sell Item Link
			userMenu.appendInsecure("<a href=\"");
			userMenu.appendInsecure(URLBuilder.create("/item/add").get());
			userMenu.appendInsecure("\">");
			userMenu.appendSecure(
					i18n(reqInfo.getLocale())
							.string("bar_userAddItemButton"));
			userMenu.appendInsecure("</a>");
			userMenu.appendSecure(" | ");
			
			if(reqInfo.getUser().isAdmin()){
				//Admin Link
				userMenu.appendInsecure("<a style=\"color:red;\" href=\"");
				userMenu.appendInsecure(URLBuilder.create("/admin/overview").get());
				userMenu.appendInsecure("\">");
				userMenu.appendSecure("Admin");
				userMenu.appendInsecure("</a>");
				userMenu.appendSecure(" | ");
			}
		}else if(userService.isUserLoggedIn() && reqInfo.getUser() == null){
			//USER NOT ACTIVE!
			userMenu.appendInsecure(i18n(reqInfo.getLocale()).string("bar_userDisabled"));
			userMenu.appendSecure(" | ");
		}
		
		if(userService.isUserLoggedIn()){
			//Logout Link
			userMenu.appendInsecure("<a href=\"");
			userMenu.appendInsecure(
					userService.createLogoutURL("/newest"));
			userMenu.appendInsecure("\">");
			userMenu.appendSecure(
					i18n(reqInfo.getLocale())
							.string("bar_userLogoutButton"));
			userMenu.appendInsecure("</a>");
		}else{
			//Login Link
			userMenu.appendInsecure("<a href=\"");
			userMenu.appendInsecure(
					userService.createLoginURL("/user/profile"));
			userMenu.appendInsecure("\">");
			userMenu.appendSecure(
					i18n(reqInfo.getLocale()).string("bar_userLoginButton"));
			userMenu.appendInsecure("</a>");
		}
		
		this.setHTML("userMenu", userMenu.toString());
		
		// Languages Dropdown List
		I18NLocale[] locales = Settings.Locales.all;
		SecureStringBuilder localesDropdownBuffer = new SecureStringBuilder();
		for (I18NLocale locale : locales) {
			localesDropdownBuffer.appendInsecure("<option value=\"");
			localesDropdownBuffer.appendSecure(locale.getLocale());
			localesDropdownBuffer.appendInsecure("\" ");
			if (locale.equals(reqInfo.getLocale()))
				localesDropdownBuffer.appendInsecure("selected=\"selected\"");
			localesDropdownBuffer.appendInsecure(">");
			localesDropdownBuffer.appendSecure(i18n(reqInfo.getLocale())
					.string("language_" + locale.getLocale()));
			localesDropdownBuffer.appendInsecure("</option>");
		}
		this.setHTML("languageDropdownOptions",
				localesDropdownBuffer.toString());

		//Init head
		this.setHTML("head", "");
	}

	public void addJSScript(String fileName) {
		jsFiles.add(fileName);
		reAddHead();
	}
	
	private void reAddHead(){
		SecureStringBuilder buffer = new SecureStringBuilder();
		for(String jsFile:jsFiles){
			buffer.appendInsecure("<script type=\"text/javascript\" src=\"/js/");
			buffer.appendInsecure(jsFile);
			buffer.appendInsecure("\"></script>");
		}
		this.setHTML("head", buffer.toString());
	}

	public void setTitle(String title) {
		this.setText("title", title);
	}
	
	public void setSearchInput(String text){
		this.setText("searchQuery", text);
	}

}
